<!--
 * @Description: 公司设置模块
 * @Author: yue
 * @Date: 2022-08-31 14:23:06
 * @LastEditors: yue
 * @LastEditTime: 2022-09-04 13:43:23
-->

<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- NOTE：卡片 -->
      <el-card>
        <!-- NOTE：tab栏 -->
        <el-tabs>
          <!-- NOTE：tab栏一 -->
          <el-tab-pane label="角色管理">
            <!-- NOTE：按钮 -->
            <el-row style="height: 60px" type="flex" align="middle">
              <el-button type="primary" size="small" icon="el-icon-plus" @click="showDialog = true">新增角色</el-button>
            </el-row>

            <!-- NOTE：表格 -->
            <el-table :data="list" border="">
              <!-- NOTE：第一列 -->
              <el-table-column type="index" label="序号" width="120"></el-table-column>
              <!-- NOTE：第二列 -->
              <el-table-column prop="name" label="角色名称" width="240"></el-table-column>
              <!-- NOTE：第三列 -->
              <el-table-column prop="description" label="描述"></el-table-column>
              <!-- NOTE：第四列 -->
              <el-table-column label="操作">
                <template slot-scope="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- NOTE：分页 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <el-pagination :current-page="page.page" :page-size="page.pagesize" :total="page.total" layout="prev, pager, next" @current-change="changePage"></el-pagination>
            </el-row>

            <!-- NOTE：弹出层 -->
            <el-dialog title="编辑角色" :visible="showDialog" @close="btnCancel">
              <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
                <el-form-item label="角色名称" prop="name">
                  <el-input v-model="roleForm.name"></el-input>
                </el-form-item>
                <el-form-item label="角色描述">
                  <el-input v-model="roleForm.description"></el-input>
                </el-form-item>
              </el-form>
              <el-row slot="footer" type="flex" justify="center" align="middle">
                <el-col :span="6">
                  <el-button size="small" @click="btnCancel">取消</el-button>
                  <el-button size="small" type="primary" @click="btnOk">确定</el-button>
                </el-col>
              </el-row>
            </el-dialog>
          </el-tab-pane>

          <!-- NOTE：tab栏二 -->
          <el-tab-pane label="公司信息">
            <!-- NOTE：提示信息 -->
            <el-alert title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改" type="info" show-icon :closeable="false"></el-alert>

            <!-- NOTE：表单 -->
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width: 400px"></el-input>
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width: 400px"></el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width: 400px"></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width: 400px"></el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole } from '@/api/setting'

export default {
  data() {
    return {
      list: [], // 员工列表
      page: {
        page: 1, // 页码
        pagesize: 2, // 每页条数
        total: 0 // 数据总数
      },
      showDialog: false, // 显示弹出层
      formData: {}, // 员工列表数据
      roleForm: {}, // 弹出层数据
      rules: {
        name: [{ required: true, trigger: 'blur', message: '角色名不能为空' }]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  mounted() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 获取角色列表信息
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    // 分页信息
    changePage(newPage) {
      this.page.page = newPage
      this.getRoleList()
    },
    // 获取公司信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    // 删除角色
    async deleteRole(id) {
      try {
        await this.$confirm('确认删除？')
        await deleteRole(id)
        this.$message.success('删除成功')
        this.getRoleList()
      } catch (err) {
        console.log(err)
      }
    },
    // 编辑角色
    async editRole(id) {
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true
    },
    // 取消编辑
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    // 确认编辑
    async btnOk() {
      try {
        await this.$refs.roleForm.validateField('name')
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
        } else {
          await addRole(this.roleForm)
        }
        this.getRoleList()
        this.showDialog = false
        this.$message.success('操作成功')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style></style>
